/**
 * Created by tdzl2003 on 10/15/16.
 */

import {
  EditorState,
  Modifier,
} from 'draft-js';

import {getAlign, setContentAlign} from '../align';

export default function keyCommandInsertNewline(editorState) {
  const oldAlign = getAlign(editorState);
  const splitedContentState = Modifier.splitBlock(editorState.getCurrentContent(), editorState.getSelection(), oldAlign);
  const selectionAfter = splitedContentState.getSelectionAfter();
  let contentState = setContentAlign(splitedContentState, selectionAfter, oldAlign);

  // Remove blocktype if it's a header.
  const blockType = contentState.getBlockForKey(selectionAfter.getFocusKey()).getType();
  if (/^header\-/.test(blockType)) {
    contentState = Modifier.setBlockType(contentState, selectionAfter, 'unstyled');
  }

  return EditorState.push(editorState, contentState, 'split-block');
}

